/*
* Copyright (c) 2009, Active Web Solutions Limited
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3- The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY Active Web Solutions Limited ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Active Web Solutions Limited BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.aws.jazure.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

/**
 * A Helper class to parse and format dates in ISO8601 format using JODA
 * 
 * @author dyaa.albakour
 *
 */
public class ISO8601Helper {

	// ref: http://www.w3.org/TR/NOTE-datetime
	
	// Current implementation uses the JODA DateTime API.
	
	public static Date parseISO8061(String str) {
		// TODO find out a way to parse dates not liked by JODA
		
		DateTimeFormatter fmt =  ISODateTimeFormat.dateTime();		
		
		try{
			DateTime dt = fmt.parseDateTime(str);
			return dt.toDate();
		}
		// for some instances like 2009-07-30T11:38:40Z
		// where no parts of seconds are showing up,
		// 	Joda is not able to parse these dates.
		catch(IllegalArgumentException e ){
			return new Date();
		}
				
	}	
	
	public static String formatISO8061(Date date){		
		DateTime jodaDate = new DateTime(date);		
		DateTimeFormatter fmt =  ISODateTimeFormat.dateTime();	
		return jodaDate.toString(fmt);		
		
		/*StringBuilder sb = new StringBuilder();
		
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS");		
		sb.append( fmt.format(date) );
		
		// Now add the Time Zone information as specified in ISO 8061
		fmt = new SimpleDateFormat("Z");		
		String tz = fmt.format(date);
		if(tz.equals("+0000")){
			sb.append("Z");
		}else{
			sb.append( tz.substring(0,3));
			sb.append(":");
			sb.append(tz.substring(3));
		}		
		return sb.toString();*/
	}
}
